/**
 * 
 */
package com.hs.core.utils;

/**
 * @author shl
 *
 */
import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.util.Properties;   
  
import org.apache.commons.logging.Log;   
import org.apache.commons.logging.LogFactory;   
  
/**  
 * @name PropertiesUtil  
*/  
public class PropertiesUtil {   
    private static Log log = LogFactory.getLog(PropertiesUtil.class);//日志输出对象   
    private static String filePath;   
    private Properties objProperties; //属性对象   
       
    /**  
     * @name PropertiesUtil  
     * @title 构造函数  
     * @desc 加载属性资源文件  
     * @param String,boolean  
     * @return   
     * @throws Exception   
     */  
    public PropertiesUtil(String filePath) throws Exception {   
        this.filePath = filePath;   
        File file = new File(filePath);   
        FileInputStream inStream = new FileInputStream(file);   
        try{   
            objProperties = new Properties();   
            objProperties.load(inStream);   
        }   
        catch(FileNotFoundException e){   
            log.error("未找到属性资源文件!");   
            e.printStackTrace();   
            throw e;   
        }   
        catch(Exception e){   
            log.error("读取属性资源文件发生未知错误!");   
            e.printStackTrace();   
            throw e;   
        }finally{   
            inStream.close();   
        }   
    }   
       
    /**  
     * @name savefile  
     * @title 持久化属性文件  
     * @desc 使用setValue()方法后，必须调用此方法才能将属性持久化到存储文件中  
     * @param String, String  
     * @return   
     * @throws Exception  
     */  
    public void savefile(String desc) throws Exception{   
        FileOutputStream outStream = null;   
        try{   
            File file = new File(filePath);   
            outStream = new FileOutputStream(file);   
            objProperties.store(outStream, desc);//保存属性文件   
        }catch(Exception e){   
            log.error("保存属性文件出错.");   
            e.printStackTrace();   
            throw e;   
        }finally{   
            outStream.close();   
        }   
           
    }   
    /**  
     * @name getVlue  
     * @title 获取属性值  
     * @desc 指定Key值，获取value  
     * @param String  
     * @return String  
     */  
    public Properties getProperties(){   
    	 
        return this.objProperties;   
    }   
    /**  
     * @name getVlue  
     * @title 获取属性值  
     * @desc 指定Key值，获取value  
     * @param String  
     * @return String  
     */  
    public String getValue(String key){   
        return objProperties.getProperty(key);   
    }   
       
    /**  
     * @name getVlue  
     * @title 获取属性值,支持缺省设置  
     * @desc 重载getValue()方法；指定Key值，获取value并支持缺省值  
     * @param String  
     * @return String  
     */  
    public String getValue(String key, String defaultValue){   
        return objProperties.getProperty(key, defaultValue);   
    }   
       
    /**  
     * @name removeVlue  
     * @title 删除属性  
     * @desc 根据Key,删除属性  
     * @param String  
     * @return   
     */  
    public void removeValue(String key){   
        objProperties.remove(key);   
    }   
       
    /**  
     * @name setValue  
     * @title 设置属性  
     * @desc   
     * @param String,String  
     * @return   
     */  
    public void setValue(String key, String value){   
        objProperties.setProperty(key, value);   
    }   
       
    /**  
     * @name printAllVlue  
     * @title 打印所有属性值  
     * @desc   
     * @param   
     * @return   
     */  
    public void printAllVlue(){   
         objProperties.list(System.out);   
    }   
}  
